﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using EQT.Model;

public partial class EquipRemind : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaEquip dalEquip = new DaEquip();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            txtStart.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            txtEnd.Text = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
            
            AspNetPager.PageSize = 15;
            this.BtnQuery_Click(null, null);
        }
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    { 
        //这里查询设备信息并绑定到gridview上。
        int pagecount = 0;
        string strCondition = "where 1=1 ";
        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }

        DateTime outdate = new DateTime();

        if (!string.IsNullOrEmpty(TxtEquipID.Text.Trim()))
        {
            strCondition = strCondition + "and  Equip_ID='" + TxtEquipID.Text.Trim() + "'";
        }
        if (!string.IsNullOrEmpty(txtStart.Text.Trim()))
        {
            if(DateTime.TryParse(txtStart.Text, out outdate))
            {
                strCondition = strCondition + " and  shoulddate>=cast('" + txtStart.Text.Trim() + "' as datetime)";
            }
            else
            {
                OperationResult.Text = "日期格式有误，请输入正确的日期查询条件！";
                divResult.Visible = true;
                return;
            }
        }
        if (!string.IsNullOrEmpty(txtEnd.Text.Trim()))
        {
            if (DateTime.TryParse(txtEnd.Text,out outdate))
            {
                strCondition = strCondition + " and  shoulddate<=cast('" + txtEnd.Text.Trim() + "' as datetime)";
            }
            else
            {
                OperationResult.Text = "日期格式有误，请输入正确的日期查询条件！";
                divResult.Visible = true;
                return;
            }
        }

        this.GridView.DataSource = dalEquip.GetEquipmentReminds(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        GridView.DataBind();

        AspNetPager.RecordCount = pagecount;
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
　    if (e.Row.RowType == DataControlRowType.DataRow)
　    {
         if (DateTime.Parse(e.Row.Cells[6].Text) < DateTime.Now)
         {
             e.Row.Cells[6].BackColor = System.Drawing.Color.Red;
             e.Row.Cells[6].ForeColor = System.Drawing.Color.White;
         }
　    }
    }
}
